<?php 
namespace app\admin\controller;
use think\Request;
use app\common\controller\Base;
use app\common\model\AdminLog;
use app\common\model\AdminUser;
/**
 * @Author: YeMiao
 * @Date:   2017-08-04 10:44:08
 * @Last Modified by:   YeMiao
 * @Last Modified time: 2017-09-11 17:33:29
 */


class User extends Base{

	public function index(){
		$user = new AdminUser();
		$find = $user->find_user(input('session.user'));
		$this->assign('user',$find);
		return $this->fetch();
	}

	public function user_log(){
		$log = new AdminLog();
		$page['limit'] = 10;
		$list = $log->get_list($page);
		$this->assign('list',$list);
		return $this->fetch();			
	}

	public function user_img(){
		if(Request::instance()->isPost()){
			$user = new AdminUser();
			$file = request()->file('img');
			$info = $file->validate(['size'=> 1024*1024, 'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
	    if($info){
	        // 成功上传后 获取上传信息
	        // 输出 jpg
	        $data = [
	        	'id' => input('post.id'),
	        	'head_img' => $info->getSaveName()
	        ];
	        $add = $user->add_head_img($data);
	        $state = [
	        	'state' => $add,
	        	'head_img' => $info->getSaveName()
	        ];
	        return json($state);
	    }else{
	        // 上传失败获取错误信息
	    		$data = [
	    			'state' => '0',
	    			'err' => $file->getError()
	    		];
	        return json($data);
	    }
		}	
	}

	public function user_add_content(){
		if(Request::instance()->isPost()){
			$user = new AdminUser();
			$add = $user->add_user_content(input('post.'));
			return $add;
		}	
	}

	public function user_update_password(){
		if(Request::instance()->isPost()){
			$user = new AdminUser();
			if(input('post.password') == input('post.repassword')){
				$update = $user->update_user_password(input('post.'));
				return $update;
			}else{
				return json(['err' => '密码不一致']);
			}
		}	
	}
}

?>